// 默认提示语和配置
const DEFAULT_CONFIG = {
  apiKey: '',
  model: 'Qwen/Qwen2.5-7B-Instruct',
  prompts: {
    translate: '请将以下文本翻译成英文：\n',
    comment: '请对以下文本进行回复，要共情，要感同身受，要理解，要站在对方的角度思考问题，要更加口语化，不要太ai味儿，字数不超过50字：\n',
    summarize: '请总结以下文本的主要内容：\n'
  },
  customMenus: [
    // 默认没有自定义菜单
  ]
};

// 创建菜单项 HTML
function createMenuItemHTML(menu, index) {
  return `
    <div class="menu-item-container">
      <div class="menu-item-fields">
        <input type="text" 
               placeholder="菜单名称" 
               value="${menu.name || ''}" 
               class="menu-name" 
               data-index="${index}">
        <textarea placeholder="提示语模板" 
                  class="menu-prompt" 
                  data-index="${index}">${menu.prompt || ''}</textarea>
      </div>
      <button class="delete-btn" data-index="${index}">删除</button>
    </div>
  `;
}

// 更新菜单列表显示
function updateMenuList(menus) {
  const menuList = document.getElementById('menuList');
  menuList.innerHTML = menus.map((menu, index) => createMenuItemHTML(menu, index)).join('');
  
  // 添加事件监听
  menuList.querySelectorAll('.delete-btn').forEach(btn => {
    btn.addEventListener('click', (e) => {
      const index = parseInt(e.target.dataset.index);
      menus.splice(index, 1);
      updateMenuList(menus);
    });
  });
}

// 保存设置
function saveOptions() {
  const apiKey = document.getElementById('apiKey').value.trim();
  const model = document.getElementById('model').value;
  const translatePrompt = document.getElementById('translatePrompt').value.trim();
  const commentPrompt = document.getElementById('commentPrompt').value.trim();
  const summarizePrompt = document.getElementById('summarizePrompt').value.trim();
  
  // 获取自定义菜单数据
  const customMenus = Array.from(document.querySelectorAll('.menu-item-container')).map(container => ({
    name: container.querySelector('.menu-name').value.trim(),
    prompt: container.querySelector('.menu-prompt').value.trim()
  })).filter(menu => menu.name && menu.prompt);

  if (!apiKey) {
    const status = document.getElementById('status');
    status.textContent = '请输入有效的 API Key';
    status.className = 'status error';
    status.style.display = 'block';
    return;
  }

  chrome.storage.sync.set({
    apiKey: apiKey,
    model: model,
    prompts: {
      translate: translatePrompt || DEFAULT_CONFIG.prompts.translate,
      comment: commentPrompt || DEFAULT_CONFIG.prompts.comment,
      summarize: summarizePrompt || DEFAULT_CONFIG.prompts.summarize
    },
    customMenus: customMenus
  }, () => {
    const status = document.getElementById('status');
    status.textContent = '设置已保存';
    status.className = 'status success';
    status.style.display = 'block';
    setTimeout(() => {
      status.style.display = 'none';
    }, 2000);
  });
}

// 恢复设置
function restoreOptions() {
  chrome.storage.sync.get(DEFAULT_CONFIG, (items) => {
    document.getElementById('apiKey').value = items.apiKey || '';
    document.getElementById('model').value = items.model || DEFAULT_CONFIG.model;
    document.getElementById('translatePrompt').value = items.prompts.translate || DEFAULT_CONFIG.prompts.translate;
    document.getElementById('commentPrompt').value = items.prompts.comment || DEFAULT_CONFIG.prompts.comment;
    document.getElementById('summarizePrompt').value = items.prompts.summarize || DEFAULT_CONFIG.prompts.summarize;
    
    // 恢复自定义菜单
    updateMenuList(items.customMenus || []);
  });
}

// 添加菜单项
document.getElementById('addMenu').addEventListener('click', () => {
  chrome.storage.sync.get({ customMenus: [] }, (items) => {
    const menus = items.customMenus;
    menus.push({ name: '', prompt: '' });
    updateMenuList(menus);
  });
});

document.addEventListener('DOMContentLoaded', restoreOptions);
document.getElementById('save').addEventListener('click', saveOptions); 